home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / multiprocessing / synchronize.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  10KB  |  320 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. __all__ = [
  5.     'Lock',
  6.     'RLock',
  7.     'Semaphore',
  8.     'BoundedSemaphore',
  9.     'Condition',
  10.     'Event']
  11. import threading
  12. import os
  13. import sys
  14. from time import time as _time, sleep as _sleep
  15. import _multiprocessing
  16. from multiprocessing.process import current_process
  17. from multiprocessing.util import Finalize, register_after_fork, debug
  18. from multiprocessing.forking import assert_spawning, Popen
  19.  
  20. try:
  21.     from _multiprocessing import SemLock
  22. except ImportError:
  23.     raise ImportError('This platform lacks a functioning sem_open' + ' implementation, therefore, the required' + ' synchronization primitives needed will not' + ' function, see issue 3770.')
  24.  
  25. (RECURSIVE_MUTEX, SEMAPHORE) = range(2)
  26. SEM_VALUE_MAX = _multiprocessing.SemLock.SEM_VALUE_MAX
  27.  
  28. class SemLock(object):
  29.     
  30.     def __init__(self, kind, value, maxvalue):
  31.         sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
  32.         debug('created semlock with handle %s' % sl.handle)
  33.         self._make_methods()
  34.         if sys.platform != 'win32':
  35.             
  36.             def _after_fork(obj):
  37.                 obj._semlock._after_fork()
  38.  
  39.             register_after_fork(self, _after_fork)
  40.  
  41.     
  42.     def _make_methods(self):
  43.         self.acquire = self._semlock.acquire
  44.         self.release = self._semlock.release
  45.  
  46.     
  47.     def __enter__(self):
  48.         return self._semlock.__enter__()
  49.  
  50.     
  51.     def __exit__(self, *args):
  52.         return self._semlock.__exit__(*args)
  53.  
  54.     
  55.     def __getstate__(self):
  56.         assert_spawning(self)
  57.         sl = self._semlock
  58.         return (Popen.duplicate_for_child(sl.handle), sl.kind, sl.maxvalue)
  59.  
  60.     
  61.     def __setstate__(self, state):
  62.         self._semlock = _multiprocessing.SemLock._rebuild(*state)
  63.         debug('recreated blocker with handle %r' % state[0])
  64.         self._make_methods()
  65.  
  66.  
  67.  
  68. class Semaphore(SemLock):
  69.     
  70.     def __init__(self, value = 1):
  71.         SemLock.__init__(self, SEMAPHORE, value, SEM_VALUE_MAX)
  72.  
  73.     
  74.     def get_value(self):
  75.         return self._semlock._get_value()
  76.  
  77.     
  78.     def __repr__(self):
  79.         
  80.         try:
  81.             value = self._semlock._get_value()
  82.         except Exception:
  83.             value = 'unknown'
  84.  
  85.         return '<Semaphore(value=%s)>' % value
  86.  
  87.  
  88.  
  89. class BoundedSemaphore(Semaphore):
  90.     
  91.     def __init__(self, value = 1):
  92.         SemLock.__init__(self, SEMAPHORE, value, value)
  93.  
  94.     
  95.     def __repr__(self):
  96.         
  97.         try:
  98.             value = self._semlock._get_value()
  99.         except Exception:
  100.             value = 'unknown'
  101.  
  102.         return '<BoundedSemaphore(value=%s, maxvalue=%s)>' % (value, self._semlock.maxvalue)
  103.  
  104.  
  105.  
  106. class Lock(SemLock):
  107.     
  108.     def __init__(self):
  109.         SemLock.__init__(self, SEMAPHORE, 1, 1)
  110.  
  111.     
  112.     def __repr__(self):
  113.         
  114.         try:
  115.             if self._semlock._is_mine():
  116.                 name = current_process().name
  117.                 if threading.current_thread().name != 'MainThread':
  118.                     name += '|' + threading.current_thread().name
  119.                 
  120.             elif self._semlock._get_value() == 1:
  121.                 name = 'None'
  122.             elif self._semlock._count() > 0:
  123.                 name = 'SomeOtherThread'
  124.             else:
  125.                 name = 'SomeOtherProcess'
  126.         except Exception:
  127.             name = 'unknown'
  128.  
  129.         return '<Lock(owner=%s)>' % name
  130.  
  131.  
  132.  
  133. class RLock(SemLock):
  134.     
  135.     def __init__(self):
  136.         SemLock.__init__(self, RECURSIVE_MUTEX, 1, 1)
  137.  
  138.     
  139.     def __repr__(self):
  140.         
  141.         try:
  142.             if self._semlock._is_mine():
  143.                 name = current_process().name
  144.                 if threading.current_thread().name != 'MainThread':
  145.                     name += '|' + threading.current_thread().name
  146.                 count = self._semlock._count()
  147.             elif self._semlock._get_value() == 1:
  148.                 (name, count) = ('None', 0)
  149.             elif self._semlock._count() > 0:
  150.                 (name, count) = ('SomeOtherThread', 'nonzero')
  151.             else:
  152.                 (name, count) = ('SomeOtherProcess', 'nonzero')
  153.         except Exception:
  154.             (name, count) = ('unknown', 'unknown')
  155.  
  156.         return '<RLock(%s, %s)>' % (name, count)
  157.  
  158.  
  159.  
  160. class Condition(object):
  161.     
  162.     def __init__(self, lock = None):
  163.         if not lock:
  164.             pass
  165.         self._lock = RLock()
  166.         self._sleeping_count = Semaphore(0)
  167.         self._woken_count = Semaphore(0)
  168.         self._wait_semaphore = Semaphore(0)
  169.         self._make_methods()
  170.  
  171.     
  172.     def __getstate__(self):
  173.         assert_spawning(self)
  174.         return (self._lock, self._sleeping_count, self._woken_count, self._wait_semaphore)
  175.  
  176.     
  177.     def __setstate__(self, state):
  178.         (self._lock, self._sleeping_count, self._woken_count, self._wait_semaphore) = state
  179.         self._make_methods()
  180.  
  181.     
  182.     def __enter__(self):
  183.         return self._lock.__enter__()
  184.  
  185.     
  186.     def __exit__(self, *args):
  187.         return self._lock.__exit__(*args)
  188.  
  189.     
  190.     def _make_methods(self):
  191.         self.acquire = self._lock.acquire
  192.         self.release = self._lock.release
  193.  
  194.     
  195.     def __repr__(self):
  196.         
  197.         try:
  198.             num_waiters = self._sleeping_count._semlock._get_value() - self._woken_count._semlock._get_value()
  199.         except Exception:
  200.             num_waiters = 'unknown'
  201.  
  202.         return '<Condition(%s, %s)>' % (self._lock, num_waiters)
  203.  
  204.     
  205.     def wait(self, timeout = None):
  206.         if not self._lock._semlock._is_mine():
  207.             raise AssertionError('must acquire() condition before using wait()')
  208.         None._sleeping_count.release()
  209.         count = self._lock._semlock._count()
  210.         for i in xrange(count):
  211.             self._lock.release()
  212.         
  213.         
  214.         try:
  215.             self._wait_semaphore.acquire(True, timeout)
  216.         finally:
  217.             self._woken_count.release()
  218.             for i in xrange(count):
  219.                 self._lock.acquire()
  220.             
  221.  
  222.  
  223.     
  224.     def notify(self):
  225.         if not self._lock._semlock._is_mine():
  226.             raise AssertionError('lock is not owned')
  227.         if not not None._wait_semaphore.acquire(False):
  228.             raise AssertionError
  229.         if self._woken_count.acquire(False):
  230.             res = self._sleeping_count.acquire(False)
  231.             if not res:
  232.                 raise AssertionError
  233.         if self._sleeping_count.acquire(False):
  234.             self._wait_semaphore.release()
  235.             self._woken_count.acquire()
  236.             self._wait_semaphore.acquire(False)
  237.  
  238.     
  239.     def notify_all(self):
  240.         if not self._lock._semlock._is_mine():
  241.             raise AssertionError('lock is not owned')
  242.         if not not None._wait_semaphore.acquire(False):
  243.             raise AssertionError
  244.         if self._woken_count.acquire(False):
  245.             res = self._sleeping_count.acquire(False)
  246.             if not res:
  247.                 raise AssertionError
  248.         sleepers = 0
  249.         while self._sleeping_count.acquire(False):
  250.             self._wait_semaphore.release()
  251.             sleepers += 1
  252.         if sleepers:
  253.             for i in xrange(sleepers):
  254.                 self._woken_count.acquire()
  255.             
  256.             while self._wait_semaphore.acquire(False):
  257.                 pass
  258.  
  259.  
  260.  
  261. class Event(object):
  262.     
  263.     def __init__(self):
  264.         self._cond = Condition(Lock())
  265.         self._flag = Semaphore(0)
  266.  
  267.     
  268.     def is_set(self):
  269.         self._cond.acquire()
  270.         
  271.         try:
  272.             if self._flag.acquire(False):
  273.                 self._flag.release()
  274.                 return True
  275.             return None
  276.         finally:
  277.             self._cond.release()
  278.  
  279.  
  280.     
  281.     def set(self):
  282.         self._cond.acquire()
  283.         
  284.         try:
  285.             self._flag.acquire(False)
  286.             self._flag.release()
  287.             self._cond.notify_all()
  288.         finally:
  289.             self._cond.release()
  290.  
  291.  
  292.     
  293.     def clear(self):
  294.         self._cond.acquire()
  295.         
  296.         try:
  297.             self._flag.acquire(False)
  298.         finally:
  299.             self._cond.release()
  300.  
  301.  
  302.     
  303.     def wait(self, timeout = None):
  304.         self._cond.acquire()
  305.         
  306.         try:
  307.             if self._flag.acquire(False):
  308.                 self._flag.release()
  309.             else:
  310.                 self._cond.wait(timeout)
  311.             if self._flag.acquire(False):
  312.                 self._flag.release()
  313.                 return True
  314.             return None
  315.         finally:
  316.             self._cond.release()
  317.  
  318.  
  319.  
  320.